package com.sxt.servlet;

import com.sxt.common.Constant;
import com.sxt.common.Result;
import com.sxt.pojo.User;
import com.sxt.service.IVisitLogService;
import com.sxt.service.impl.VisitLogServiceImpl;
import com.sxt.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Description:
 * @author: Mr.T
 * @date 2020-11-06 10:33
 */
@WebServlet("/visit.do")
public class VisitLogServlet  extends HttpServlet {

    private IVisitLogService visitLogService = new VisitLogServiceImpl();


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String service = req.getParameter("service");
        try {
            //此时 service 值与方法名相同  则可以根据service找到方法
            Method method = this.getClass().getDeclaredMethod(service, HttpServletRequest.class, HttpServletResponse.class);
            //进行方法调用
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            throw  new RuntimeException("找不到方法:"+service);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 新增拜访记录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String customerId = req.getParameter("customerId");
        String name = req.getParameter("name");
        String visitTime = req.getParameter("visitTime");
        String descp = req.getParameter("descp");
        //从session 获取当前业务员的ID  和  名称
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute(Constant.SESSION_CURRENT_USER_KEY);
        //
        Result rs = visitLogService.add(customerId,name,visitTime,descp,user);
        //将数据输出
        RespUtil.writer(rs,resp);
    }
}
